﻿using HK.Core.Counter.Protocol;

namespace HK.Core.Counter {

    /// <summary>
    /// 时间计数器.
    /// </summary>
    public class TimeCounter : Counter<TimeCounter, float>, ITimeCounter 
    {

        /// <summary>
        /// 创建计数器.
        /// </summary>
        /// <param name="iOnCounterUpdate">计时器更新回调.</param>
        /// <param name="iDstValue">目标值(默认:30秒).</param>
        /// <param name="iMode">模式（默认为：倒计时）.</param>
        /// <param name="iLoop">循环计数标志位.</param>
        /// <param name="iInterval">更新时间间隔(单位:1000毫微秒).</param>
        public static ITimeCounter Create(
            CounterUpdated iOnCounterUpdate, float iDstValue = 30.0f, 
            CounterMode iMode = CounterMode.CountDown, 
            bool iLoop = false, long iInterval = 1000L)
        {
            return Create(CounterType.TimeCounter, iDstValue, iOnCounterUpdate, iMode, iLoop, iInterval);
        }

#region Implement

        /// <summary>
        /// 是否已经超过计数.
        /// </summary>
        /// <returns><c>true</c>, 已经超过计数, <c>false</c> 尚未超过计数.</returns>
        protected override bool IsCountOver() {
            switch (Mode) {
                case CounterMode.CountDown:
                {
                    if (Value <= 0.0f) {
                        return true;
                    }
                }
                    break;
                case CounterMode.CountUp:
                {
                    if (Value >= DstValue) {
                        return true;
                    }
                }
                    break;
                default:
                    break;
            }
            return false;
        }

        /// <summary>
        /// 根据变化值更新计数器.
        /// </summary>
        /// <param name="iDeltaValue">变化值.</param>
        protected override void UpdateCounter(float iDeltaValue) {
			
            switch (Mode) {
                case CounterMode.CountDown:
                {
                    Value -= iDeltaValue;
                }
                    break;
                case CounterMode.CountUp:
                {
                    Value += iDeltaValue;
                }
                    break;
                default:
                    break;
            }
            // Info ("UpdateCounter():Type::{0} Mode::{1} State::{2} Value::{3}/{4}",
            //     Type, Mode, State, Value, DstValue);
        }

#endregion
    }

}